package com.threeidiots.transport.dao.hibernate;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.threeidiots.transport.dao.UserDAO;
import com.threeidiots.transport.entities.Roles;
import com.threeidiots.transport.entities.User;


public class UserDAOImpl implements UserDAO {
	
	private HibernateTemplate hibernateTemplate;
	private static Logger LOGGER = Logger.getLogger(UserDAOImpl.class);
	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public User findByLogin(String userCode, String password) {
		User user= null;
		List<User> usersList= hibernateTemplate.find("user.findByLogin", new Object[]{userCode,password});
		if(usersList != null){
			user = usersList.get(0);
		}
		LOGGER.info("findByLogin:" +user);
		return user;
	}

	@Override
	public List<User> getAllUsers() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Roles> getUserRoles(String userCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int saveUser(User user) {
		hibernateTemplate.save(user);
		LOGGER.info("saveUser:" +user);
		return user.getUserId().intValue();
	}
	
	
	public static void main(String[] args) {
		Resource res = new FileSystemResource("applicationContext.xml");
		XmlBeanFactory factory = new XmlBeanFactory(res);
		// of course, an ApplicationContext is just a BeanFactory
		UserDAO dao =(UserDAOImpl)factory.getBean("userDAOImpl");
		LOGGER.info(dao);
	}

}
